<template>
	<view class="wrap">
		<view class="pic">
			<image src="https://ya30images.hbwlife.com/wxmp/subMessage.png" mode=""></image>
		</view>
		<view class="tips">
			订阅消息可及时接收审核结果
		</view>
		<view class="btn" @click="fnMessage">
			订阅消息
		</view>
	</view>
</template>



<script>
	import {
		request,
		requestData
	} from '@/utils/http.js'
	export default {
		data() {
			return {

			}
		},
		onLoad(option) {},
		onShow() {},
		onUnload() {

		},
		methods: {
			fnMessage() {
				uni.getSetting({
					withSubscriptions: true,
					success(res) {
						if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
							if (res.subscriptionsSetting.itemSettings) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
								//@ts-ignore
								let moIdState = res.subscriptionsSetting.itemSettings[
									'dyUG7077ENKvbD3HuJSNzqIcDpPPvtla7NCvrZMfCqE'
								]; // 用户同意的消息模板id
								if (moIdState === 'accept') {
									uni.requestSubscribeMessage({
										tmplIds: ['dyUG7077ENKvbD3HuJSNzqIcDpPPvtla7NCvrZMfCqE'],
										success(res) {
											console.log("🚀 ~ success ~ 不会再拉起推送消息的授权,订阅成功")
										},
									})
								} else if (moIdState === 'reject') {
									console.log('拒绝了消息推送');
									uni.showToast({
										title: '拒绝了消息推送',
									});
									uni.openSetting({
										withSubscriptions: true,
									});
								} else if (moIdState === 'ban') {
									uni.showToast({
										title: '已被后台封禁',
									})
								}
							} else {
								//提示开始订阅消息
								uni.requestSubscribeMessage({
									// 此处填写申请的订阅消息模板的ID
									tmplIds: ['c6DX7UbTOL1hciWxbdeTjBr-FFWQrFXO5z9XXXXXXXX',
										'ftpGE2XLY4rgXXxFWQrFXO5z9XXXXXXXX'
									],
									success(res) {
										console.log("uni.requestSubscribeMessage" + JSON.stringify(res))
									}
								})
							}
						} else {
							uni.openSetting({
								withSubscriptions: true,
							})
						}
					}
				})


			},
			// fnMessage() {
			// 	wx.requestSubscribeMessage({
			// 		tmplIds: ['dyUG7077ENKvbD3HuJSNzqIcDpPPvtla7NCvrZMfCqE'], //消息模板ID
			// 		success(res) {
			// 			//用户同意订阅消息
			// 			console.log('用户同意订阅消息', res)
			// 			if (res.errMsg === 'requestSubscribeMessage:ok') {
			// 				if (res['dyUG7077ENKvbD3HuJSNzqIcDpPPvtla7NCvrZMfCqE'] === 'accept') {
			// 					uni.showToast({
			// 						title: '订阅成功',
			// 						icon: 'none',
			// 					})
			// 					setTimeout(() => {
			// 						uni.switchTab({
			// 							url: '/pages/my/index',
			// 						})
			// 					}, 2000)
			// 				} else if (res['dyUG7077ENKvbD3HuJSNzqIcDpPPvtla7NCvrZMfCqE'] === 'reject') {
			// 					uni.showToast({
			// 						title: '订阅失败',
			// 						icon: 'none',
			// 					})
			// 				}
			// 			}
			// 		},
			// 		fail(err) {
			// 			//用户拒绝订阅消息
			// 			console.log('用户拒绝订阅消息', err)
			// 			uni.showToast({
			// 				title: '订阅失败',
			// 				icon: 'none',
			// 			})
			// 		},
			// 	})
			// }
		}
	}
</script>

<style scoped>
	.wrap {
		width: 100%;
		height: 100vh;
		box-sizing: border-box;
		background: #fff;
		font-size: 32rpx;
		display: flex;
		flex-flow: column;
		align-items: center;
	}

	.pic {
		width: 160rpx;
		height: 160rpx;
		margin-top: 20%;
	}

	.pic image {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		height: 100%;
	}

	.tips {
		width: 90%;
		font-family: 28rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #676a6d;
		margin-top: 160rpx;

	}

	.btn {
		background: linear-gradient(to right, #c8a357, #fbde94, #c8a357);
		width: 94%;
		height: 80rpx;
		line-height: 80rpx;
		margin: 0 auto;
		margin-top: 60rpx;
		border-radius: 16rpx;
		color: #333;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	::v-deep .u-button--primary {
		border: none !important;
		// background: linear-gradient(to right, #d5ad57, #cfb58a);
		background-color: $global-color-golden;
	}
</style>